package com.idp.demo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.idp.common.base.BaseController;
import com.idp.common.model.PageModel;
import com.idp.common.model.ResultModel;
import com.idp.demo.dto.GoodsCategoryDto;
import com.idp.demo.dto.GoodsCategorySearchDto;
import com.idp.demo.service.GoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品类别Controller
 * @author King
 */
@Api(tags="商品类别")
@RestController
@RequestMapping("/goodsCategory/")
public class GoodsCategoryController extends BaseController {

	@Autowired
	private GoodsCategoryService goodsCategoryService;

	/**
	 * 分页查询
	 * @param goodsCategorySearchDto
	 * @return
	 */
	@ApiOperation(value="分页查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页数", paramType = "query", required = true, defaultValue = "1"),
		@ApiImplicitParam(name = "rows", value = "每页行数", paramType = "query", required = true, defaultValue = "10")
	})
	@PostMapping("findByPage")
	public ResultModel<PageModel<GoodsCategoryDto>> findByPage(@RequestBody GoodsCategorySearchDto goodsCategorySearchDto) {
		return ResultModel.success(goodsCategoryService.findByPage(goodsCategorySearchDto));
	}

	/**
	 * 通过id查询
	 * @param goodsCategoryDto
	 * @return
	 */
	@ApiOperation(value="通过id查询")
	@ApiOperationSupport(includeParameters = {"goodsCategoryDto.id"})
	@PostMapping("getById")
	public ResultModel<GoodsCategoryDto> getById(@RequestBody GoodsCategoryDto goodsCategoryDto) {
		return ResultModel.success(goodsCategoryService.getById(goodsCategoryDto.getId()));
	}

	/**
	 * 新增
	 * @param goodsCategoryDto
	 * @return
	 */
	@ApiOperation(value="新增")
	@ApiOperationSupport(ignoreParameters = {"goodsCategoryDto.id"})
	@PostMapping("add")
	public ResultModel add(@RequestBody GoodsCategoryDto goodsCategoryDto) {
		goodsCategoryService.add(goodsCategoryDto);
		return ResultModel.success("新增成功", null);
	}

	/**
	 * 修改
	 * @param goodsCategoryDto
	 * @return
	 */
	@ApiOperation(value="修改")
	@PostMapping("update")
	public ResultModel update(@RequestBody GoodsCategoryDto goodsCategoryDto) {
		goodsCategoryService.update(goodsCategoryDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 删除
	 * @param goodsCategoryDto
	 * @return
	 */
	@ApiOperation(value="删除")
	@ApiOperationSupport(includeParameters = {"goodsCategoryDto.id"})
	@PostMapping("delete")
	public ResultModel delete(@RequestBody GoodsCategoryDto goodsCategoryDto) {
		goodsCategoryService.delete(goodsCategoryDto.getId());
		return ResultModel.success("删除成功", null);
	}

	/**
	 * 查询所有商品类型
	 * @return
	 */
	@ApiOperation(value="查询所有商品类型")
	@PostMapping("findAll")
	public ResultModel<List<GoodsCategoryDto>> findAll() {
		return ResultModel.success(goodsCategoryService.findAll());
	}
}
